import Taro from "@tarojs/taro"
import { getCloudUrl } from "../utils"
let instance

export class CustomAudio {
  constructor(src = '') {
    const audio = Taro.createInnerAudioContext({
      useWebAudioImplement: false // 是否使用 WebAudio 作为底层音频驱动，默认关闭
    })
    audio.src = src
    return audio
  }
}

/**
 * 统一的音效管理器
 */
export class Music {
  constructor(list) {
    if (instance) return instance
    instance = this
    this.MusicList = []
    this.createdMusic(list)
  }
  createdMusic (list) {
    list.forEach(item => {
      if (item.name && this.MusicList.find(e => e.name !== item.name)) {
        this.MusicList.push(item)
      } 
    })
    this.MusicList.push(...list)
    this.MusicList.forEach(item => {
      this.addMusic(item.name, item.src)
    })
  }
  addMusic (name = '', src = '') {
    if (!name) {
      return null
    }
    if (this[name]) {
      return this[name]
    }
    this[name] = new CustomAudio(src)
    this[name].name = name
  }
  updateMusic (name) {
    if (!this[name]) {
      return null
    }
    const item = this[name]
    const src = item.src
    item.destroy()
    this[name] = new CustomAudio(src)
    this[name].name = name
    this[name].play()
  }
  getMusic(name) {
    return this[name]
  } 

  removeMusic(name) {
    if (!name) {
      return null
    }
    if (this[name]) {
      this[name].pause()
      this[name].src = ''
      this[name].load()
      this[name] = null
    }
  }
  setAudio(param = {}) {
    const {
      name,
      loop = false,
      play = false,
      pause = false,
      autoplay = false,
      currentTime,
      volume = 1
    } = param
    if (this[name]) {
      this[name].loop = loop
      this[name].autoplay = autoplay
      this[name].volume = volume
      if (currentTime !== undefined) {
        this[name].currentTime = currentTime
      }
      if (play) {
        this[name].play()
      }
      if (pause) {
        this[name].pause()
      }
    }

  }
  play(name) {
    if (!this[name]) {
      return null
    }
    return this[name].play()
  }
  pause (name) {
    if (!this[name]) {
      return null
    }
    return this[name].pause()
  }
  playShoot(name) {
    if (!this[name]) {
      return null
    }
    this[name].currentTime = 0
    return this[name].play()
  }

  createdAudio(name) {
    if (!this[name]) {
      return null
    }
    const audio = new CustomAudio(this[name].src)
    audio.autoplay = true
    audio.volume = this[name].volume
    this[name] = audio
    audio.play()
    return audio
  }
  destroy() {
    for (const name in this) {
      if (this[name]) {
        this[name].pause()
        this[name] = null
      }
    }
  }
  close () {
    for (const name in this) {
      if (this[name]?.pause) {
        this[name].pause()
      }
    }
  }
}
const musicList = [
  {
    name: 'HomeBgm',
    src: getCloudUrl('audio/home_bgm.mp3')
  },
  {
    name: 'GameBgm',
    src: getCloudUrl('audio/game_bgm.wav')
  },
  {
    name: 'StartGameBgm',
    src: getCloudUrl('audio/start_game_bgm.wav')
  },
  {
    name: 'gameClickBgm',
    src: getCloudUrl('audio/game_click_bgm.mp3')
  },
  {
    name: 'receivedBomb',
    src: getCloudUrl('audio/received_bomb.wav')
  },
  {
    name: 'explosionSound',
    src: getCloudUrl('audio/explosion_sound.wav')
  },
  {
    name: 'gameFail',
    src: getCloudUrl('audio/game_fail.mp3')
  },
  {
    name: 'goldDrops',
    src: getCloudUrl('audio/gold_drops.wav')
  }
]

/**
 * 统一管理音效
 */
export default new Music(musicList)